// page_two/sisdjadd/sisdjadd.js
let app = getApp()
let yuming = app.globalData.yuming;
let mobile = app.globalData.mobile;
let sfzza = app.globalData.sfzza;
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '四实登记',
    buttonClicked: false,
    name: '',
    phone: '',
    sfzh: '',
    sfzzm: '',
    wusfzzm: '',
    sfzfm: '',
    wusfzfm: '',
  },
  // 姓名
  name(e) {
    this.setData({
      name: e.detail.value
    })
  },
  // 手机号
  phone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 身份证号
  sfzh(e) {
    this.setData({
      sfzh: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //身份证正面
        if (aaa == 0) {
          that.setData({
            sfzzm: urls,
            wusfzzm: data.imgUrl
          });
        }
        //身份证反面
        if (aaa == 1) {
          that.setData({
            sfzfm: urls,
            wusfzfm: data.imgUrl
          });
        }
      })
    })
  },
  showModal(e) {
    var aaa = e.currentTarget.dataset.aaa
    var img = e.currentTarget.dataset.img
    this.setData({
      fdaaa: aaa,
      fdimg: img
    })
    let {
      type
    } = e.currentTarget.dataset, va = '';
    if (type == 's2') {
      va = "s2";
    }
    this.setData({
      [va]: true
    })
  },
  confirmbtn(e) {
    console.log(e)
    console.log('点击了从新上传！')
    var aaa = this.data.fdaaa
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        //身份证正面
        if (aaa == 0) {
          that.setData({
            sfzzm: urls,
            wusfzzm: data.imgUrl
          });
        }
        //身份证反面
        if (aaa == 1) {
          that.setData({
            sfzfm: urls,
            wusfzfm: data.imgUrl
          });
        }
      })
    })

  },
  cancelBtn() {
    console.log('点击了放大！')
    wx.previewImage({
      urls: [this.data.fdimg]
    })
  },
  yanzheng() {
    if (this.data.name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
    } else if (this.data.phone == '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
    } else if (!mobile.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
    } else if (this.data.sfzh == '') {
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none'
      })
    } else if (!sfzza.test(this.data.sfzh)) {
      wx.showToast({
        title: '请输入有效身份证号',
        icon: 'none'
      })
    } else if (this.data.wusfzzm == '') {
      wx.showToast({
        title: '请上传身份证正面',
        icon: 'none'
      })
    } else if (this.data.wusfzfm == '') {
      wx.showToast({
        title: '请上传身份证反面',
        icon: 'none'
      })
    } else {
      this.submit();
    }
  },
  submit() {
    let that = this;
    that.setData({
      buttonClicked: true,
    })
    this.myRequest(yuming + 'Shopthat/four_realms_add', {
      shopId: wx.getStorageSync('shopId'),
      name: that.data.name,
      mobile: that.data.phone,
      number: that.data.sfzh,
      identity_z: that.data.wusfzzm,
      identity_f: that.data.wusfzfm
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 1500)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})